package com.wjp.villagemp.controller.manage;

import com.wjp.villagemp.model.manage.CourseEntity;
import com.wjp.villagemp.model.sys.PageResult;
import com.wjp.villagemp.service.manage.CourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主题类型管理
 * @author lixr
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private CourseService courseService;

    /**
     * 分页查询所有课程信息
     * @param pageSize
     * @param page
     * @param courseNum
     * @return
     */
    @GetMapping
    public PageResult courseList(int pageSize, int page, @RequestParam(required = false) int courseNum) {
        PageResult pageResult = new PageResult();
        try {
            List<CourseEntity> listCourse = courseService.listCourse(pageSize, page * pageSize, courseNum);
            pageResult.setData(listCourse);
            Integer course = courseService.countCourse(courseNum);
            pageResult.setTotalCount(course);
        } catch (Exception e) {
            log.error("分页查询所有课程信息异常：" + e.getMessage());
        }
        return pageResult;
    }


    /**
     * 新增课程信息
     * @param courseEntity
     * @return
     */
    @PostMapping
    public Map<String, Object> insertCourse(@RequestBody CourseEntity courseEntity) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (courseEntity != null) {
                Integer key = courseService.insertCourse(courseEntity);
                if (key > 0) {
                    resMap.put("msg", "success");
                } else {
                    resMap.put("msg", "fail");
                }
            }
        } catch (Exception e) {
            log.error("新增课程信息异常：" + e.getMessage());
        }
        return resMap;
    }

    /**
     * 更新课程信息
     * @param courseEntity
     * @return
     */
    @PutMapping
    public Map<String, Object> updateCourse(@RequestBody CourseEntity courseEntity) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (courseEntity != null) {
                Integer key = courseService.updateCourse(courseEntity);
                if (key > 0) {
                    resMap.put("msg", "success");
                } else {
                    resMap.put("msg", "fail");
                }
            }
        } catch (Exception e) {
            log.error("更新课程信息异常：" + e.getMessage());
        }
        return resMap;
    }

    /**
     * 批量删除课程信息
     * @param groupId
     * @return
     */
    @DeleteMapping
    public Map<String, Object> removeCourse(@RequestBody List<Integer> groupId) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (groupId.size() >= 1) {
                boolean flag = false;
                for (Integer courseNum : groupId) {
                    Integer key = courseService.removeCourse(courseNum);
                    if (key > 0) {
                        flag = true;
                    } else {
                        flag = false;
                    }
                }
                if (flag) {
                    resMap.put("msg", "success");
                }
            }
        } catch (Exception e) {
            log.error("批量删除课程信息异常：" + e.getMessage());
        }
        return resMap;
    }

    /**
     * 查询全部课程信息
     * @return
     */
    @GetMapping("/getAllCourses")
    public List<CourseEntity> getAllThemes() {
        List<CourseEntity> courseList = null;
        try {
            courseList  = courseService.listAllCourse();
        } catch (Exception e) {
            log.error("查询全部课程信息异常：" + e.getMessage());
        }
        return courseList;
    }
}
